//
//  BLCertificateController.m
//  Byelone
//
//  Created by 乔小乔 on 2021/2/3.
//

#import "BLCertificateController.h"
#import "BLCertificate_Cell.h"
#import "BLCertificateModel.h"

#import "BLCertificateGradeVC.h"
#import "BLCertIdentityVC.h"

static NSString *reuse_BLCertificate_Cell = @"Reuse_BLCertificate_Cell";
@interface BLCertificateController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableViewList;
@property (nonatomic, strong) NSMutableArray *arrayData;
@end

@implementation BLCertificateController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的认证";
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)loadData{
    [BLHttpTool getHttpRequest:BL_USER_AUTHINFO param:@{} finish:^(NSURLSessionDataTask * _Nonnull data, NSDictionary * _Nonnull obj) {
        if ([obj[@"content"] isKindOfClass:[NSArray class]]) {
            [self.arrayData removeAllObjects];
            [self.arrayData addObjectsFromArray:obj[@"content"]];
            [self.tableViewList reloadData];
        }
    } failedBlock:^(id  _Nonnull data, NSError * _Nonnull error) {
        
    }];
}
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.arrayData[indexPath.row];
    BLCertificateItem *item = [BLCertificateItem mj_objectWithKeyValues:dict];
    
    BLCertificate_Cell *cell = [tableView dequeueReusableCellWithIdentifier:reuse_BLCertificate_Cell forIndexPath:indexPath];
    [cell reloadWithData:item];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 164;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.arrayData[indexPath.row];
    BLCertificateItem *item = [BLCertificateItem mj_objectWithKeyValues:dict];
    if ([item.authType isEqualToString:@"1"]) {//身份
        BLCertIdentityVC *vc = [[BLCertIdentityVC alloc]init];
        vc.dataModel = item;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([item.authType isEqualToString:@"2"]) {//学历
        BLCertificateGradeVC *vc = [[BLCertificateGradeVC alloc]init];
        vc.dataModel = item;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark - set&get
- (UITableView *)tableViewList{
    if (!_tableViewList) {
        _tableViewList = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, BL_WINDOW_WIDTH, BL_CONTENT_HEIGHT)];
        [self.view addSubview:_tableViewList];
        _tableViewList.backgroundColor = [UIColor clearColor];
        _tableViewList.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableViewList.delegate = self;
        _tableViewList.dataSource = self;
        if (@available(iOS 11.0, *)) {
            _tableViewList.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [_tableViewList registerNib:[UINib nibWithNibName:@"BLCertificate_Cell" bundle:nil] forCellReuseIdentifier:reuse_BLCertificate_Cell];
        _tableViewList.tableFooterView = [UIView new];
    }
    return _tableViewList;
}

- (NSMutableArray *)arrayData{
    if (!_arrayData) {
        _arrayData = [[NSMutableArray alloc]init];
    }
    return _arrayData;
}
@end
